<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-cascader
          v-model="query.cascader"
          placeholder="学校/年级/班级"
          :options="options"
          :props="{ expandTrigger: 'hover',checkStrictly: true }"
          style="width: 185px;"
          class="filter-item"
          clearable
          @change="queryHandleChange"
        />
        <el-input v-model="query.name" clearable placeholder="学生姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-date-picker
          v-model="query.gmtCreate"
          clearable
          type="daterange"
          class="date-item"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="600px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="学校" prop="school.id">
            <el-select v-model.number="form.school.id" placeholder="请选择学校" style="width: 370px" @change="gradeHandle">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="年级" prop="grade.id">
            <el-select v-model.number="form.grade.id" placeholder="请选择年级" style="width: 370px" @change="clazzHandle">
              <el-option v-for="item in grades" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="班级" prop="clazz.id">
            <el-select v-model.number="form.clazz.id" placeholder="请选择班级" style="width: 370px">
              <el-option v-for="item in clazzs" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="学生姓名" prop="name">
            <el-input v-model="form.name" style="width: 370px;" placeholder="请填写学生姓名" />
          </el-form-item>
          <el-form-item label="缴费金额(分)" prop="totalFee">
            <el-input-number
              v-model="form.totalFee"
              :controls="false"
              :min="0"
              style="text-align:left !important;  width: 370px;"
              placeholder="请填写缴费金额(分)"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="school" label="学校">
          <template slot-scope="scope">
            <span>{{ scope.row.school.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="grade" label="年级">
          <template slot-scope="scope">
            <span>{{ scope.row.grade.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="clazz" label="班级">
          <template slot-scope="scope">
            <span>{{ scope.row.clazz.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="学生姓名" />
        <el-table-column prop="totalFee" label="缴费金额">
          <template slot-scope="scope">
            <span>{{ rmbHandle(scope.row.totalFee) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreate" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtCreate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtModified" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtModified) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','record:edit','record:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSchool from '@/api/pay/school'
import crudGrade from '@/api/pay/grade'
import crudDict from '@/api/system/dict'
import crudClazz from '@/api/pay/class'
import crudRecord from '@/api/pay/record'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, school: {}, grade: {}, clazz: {}, name: null, totalFee: null, gmtCreate: null, gmtModified: null }
export default {
  name: 'Record',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '缴费记录', url: 'api/record', sort: 'id,desc', crudMethod: { ...crudRecord }})
  },
  data() {
    return {
      schools: [], clazzs: [], grades: [],
      permission: {
        add: ['admin', 'record:add'],
        edit: ['admin', 'record:edit'],
        del: ['admin', 'record:del']
      },
      rules: {
        'school.id': [
          { required: true, message: '请选择学校', trigger: 'blur' }
        ],
        'grade.id': [
          { required: true, message: '请选择年级', trigger: 'blur' }
        ],
        'clazz.id': [
          { required: true, message: '请选择班级', trigger: 'blur' }
        ],
        'name': [
          { required: true, message: '请填写学生姓名', trigger: 'blur' }
        ],
        'totalFee': [
          { required: true, message: '请填写缴费金额', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'schoolId', display_name: '学校id' },
        { key: 'gradeId', display_name: '年级id' },
        { key: 'clazzId', display_name: '班级id' },
        { key: 'name', display_name: '学生姓名' },
        { key: 'gmtCreate', display_name: '创建时间' },
        { key: 'cascader', display_name: '组合' }
      ],
      options: []
    }
  },
  mounted() {
    crudSchool.query().then(res => {
      this.options = res.content
      this.optionHandle()
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      // 获取缴费金额
      crudDict.getDictsByName('微信缴费信息').then(data => {
        this.crud.form.totalFee = data.content[0].dictDetails[1].value
      })
    },
    [CRUD.HOOK.beforeToEdit](data) {
      const form = data.form
      this.gradeHandle(form.school.id, false)
      this.clazzHandle(form.grade.id, false)
    },
    async gradeHandle(schoolId, isChange = true) {
      if (isChange) {
        this.crud.form.grade = {}
        this.crud.form.clazz = {}
      }
      const res = await crudGrade.query(schoolId)
      this.grades = res.content
    },
    async clazzHandle(gradeId, isChange = true) {
      if (isChange) {
        this.crud.form.clazz = {}
      }
      const res = await crudClazz.query(gradeId)
      this.clazzs = res.content
    },
    optionHandle() {
      this.options.forEach(school => {
        school.children = school.gradeList
        school.value = school.id
        school.label = school.name
        school.children.forEach(grade => {
          grade.value = grade.id
          grade.label = grade.name
          grade.children = grade.classList
          grade.children.forEach(clazz => {
            clazz.value = clazz.id
            clazz.label = clazz.name
          })
        })
      })
    },
    queryHandleChange(value) {
      this.query.schoolId = value[0]
      this.query.gradeId = value[1]
      this.query.clazzId = value[2]
    },
    // 分转换为浮点
    rmbHandle(fee) {
      if (fee !== null) {
        let floatNum = fee.slice(-2)
        const regExp = new RegExp(`(\\S*)${floatNum}`)
        let integerNum = fee.match(regExp)[1]
        // 如果浮点位小于2,高位加个0
        if (floatNum < 2) {
          floatNum = '0' + floatNum
        }
        if (integerNum === '') {
          integerNum = '0'
        }
        return integerNum + '.' + floatNum
      }
    }
  }
}
</script>

<style scoped>

</style>
